package lovo01.homework04;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 拷贝
 */
public class Copy{
    /**
     * 文件拷贝
     * @param source 源文件路径
     * @param dist 目标文件路径
     */
    public void copy(String source,String dist){
        String str = "";
        byte[] bs = null;
        byte[] bis = new byte[1024];
        FileInputStream os = null;
        FileOutputStream ois = null;

        try {
            //建立流对象
            os = new FileInputStream(source);
            int n = 0;
            //读取数据
            while ((n = os.read(bis)) != -1){
                str += new String(bis,0,n);
            }

            bs = str.getBytes();
            //建立输出流
            ois = new FileOutputStream(dist);
            //输出数据
            ois.write(bs);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Copy copy = new Copy();
        copy.copy("src/lovo01/ATM04/1.txt","src/lovo01/homework04/1.txt");
    }
}
